local skel = fk.CreateSkill {
  name = "emo__tuibing",
}

Fk:loadTranslationTable{
  ["emo__tuibing"] = "推兵",
  [":emo__tuibing"] = "每名角色若未使用过装备牌，其可以将装备牌当【桃】或【杀】使用。结算后你可以令唯一目标使用此装备牌。出牌阶段开始时，若所有角色均使用过装备牌，你可以视为对所有其他角色使用【瞒天过海】。",

  ["#emo__tuibing-equip"] = "推兵：你可以令 %dest 使用 %arg",
  ["#emo__tuibing-underhanding"] = "推兵：你可以视为对所有其他角色使用【瞒天过海】",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return target == player and player.phase == Player.Play and player:hasSkill(skel.name)
    and table.every(player.room.alive_players, function (p) return p :getMark("emo__tuibing_used") ~= 0 end)
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = skel.name,  prompt = "#emo__tuibing-underhanding"  })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local card = Fk:cloneCard("underhanding")
    card.skillName = skel.name
    if player:prohibitUse(card) then return end
    local tos = table.filter(room:getOtherPlayers(player), function (to) return not player:isProhibited(to, card) end)
    if #tos == 0 then return end
    room:useCard{from = player, tos = tos, card = card }
  end,
})

skel:addEffect(fk.CardUsing, {
  can_refresh = function (self, event, target, player, data)
    return target == player and data.card.type == Card.TypeEquip and player:getMark("emo__tuibing_used") == 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "emo__tuibing_used", 1)
    player.room:handleAddLoseSkills(player, "-emo__tuibing_viewas&")
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  if not is_start then -- 给我狠狠查记录，谁用了装备
    local users = {}
    player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
      local use = e.data
      if use.card.type == Card.TypeEquip and use.from:isAlive() then
        table.insertIfNeed(users, use.from)
      end
      return #users >= #room.alive_players
    end, Player.HistoryGame)
    for _, p in ipairs(users) do
      room:setPlayerMark(p, "emo__tuibing_used", 1)
    end
  end
  for _, p in ipairs(room.alive_players) do
    if p:getMark("emo__tuibing_used") == 0 then
      room:handleAddLoseSkills(p, "emo__tuibing_viewas&")
    end
  end
end)

--- 我噶了要把所有角色的副技能删除
skel:addLoseEffect(function (self, player, is_death)
  local room = player.room
  if table.every(room.alive_players, function (p) return not p:hasSkill(self.name) end) then
    for _, p in ipairs(room.alive_players) do
      room:handleAddLoseSkills(p, "-emo__tuibing_viewas&")
    end
  end
end)

local skel2 = fk.CreateSkill {
  name = "emo__tuibing_viewas&",
}

--- 检查是否有人拥有此主技能
local function ownerCheck()
  for _, p in ipairs(Fk:currentRoom().alive_players) do
    if p:hasSkill(skel.name) then
      return true
    end
  end
  return false
end

skel2:addEffect("viewas", {
  pattern = "slash,peach",
  prompt = "#emo__tuibing_viewas",
  interaction = function (self, player)
    local all_names = {"slash", "peach"}
    local names = player:getViewAsCardNames(skel.name, all_names)
    if #names == 0 then return end
    return UI.CardNameBox {choices = names}
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeEquip
  end,
  view_as = function (self, player, cards)
    if not self.interaction.data then return end
    if #cards == 1 then
      local card = Fk:cloneCard(self.interaction.data)
      card:addSubcards(cards)
      card.skillName = skel.name
      return card
    end
  end,
  after_use = function(self, player, use)
    local room = player.room
    if not (use.tos and #use.tos == 1 and use.tos[1]:isAlive() ) then return end
    local to = use.tos[1]
    local cid = use.card:getEffectiveId()
    if cid and table.contains({Card.Processing, Card.DiscardPile}, room:getCardArea(cid)) then
      local card = Fk:getCardById(cid, true)
      if card.type == Card.TypeEquip and to:canUseTo(card, to) then
        for _, p in ipairs(room:getAlivePlayers()) do
          if p:hasSkill(skel.name) and room:askToSkillInvoke(p, {skill_name = skel.name,
          prompt = "#emo__tuibing-equip::"..to.id ..":"..card:toLogString() }) then
            room:useCard{from = to, tos = {to}, card = card }
            break
          end
        end
      end
    end
  end,
  enabled_at_play = function(self, player)
    return player:getMark("emo__tuibing_used") == 0 and ownerCheck()
  end,
  enabled_at_response = function (self, player, response)
    if not response and not player:isNude() and player:getMark("emo__tuibing_used") == 0 and ownerCheck() then
      local all_names = {"slash", "peach"}
      return #player:getViewAsCardNames(skel.name, all_names) > 0
    end
  end,
})


Fk:loadTranslationTable{
  ["emo__tuibing_viewas&"] = "推兵",
  [":emo__tuibing_viewas&"] = "若你未使用过装备牌，可以将装备牌当【桃】或【杀】使用",
  ["#emo__tuibing_viewas"] = "推兵：将装备牌当【桃】或【杀】使用",
}


return {skel, skel2}
